package basic.study.leetcode;

/**
 * @ClassName Mid64
 * @Description 最小路径和
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/23 15:18
 * @Version 1.0
 */
public class Mid64 {
    /**
     * dp
     */
    class Solution {
        public int minPathSum(int[][] grid) {
            int rowNum = grid.length;
            int columnNum = grid[0].length;
            int[][] dp = new int[rowNum][columnNum];
            //初始化
            dp[0][0] = grid[0][0];
            for (int i = 1; i < rowNum; i++) {
                dp[i][0] = dp[i-1][0] + grid[i][0];
            }
            for (int i = 1; i < columnNum; i++) {
                dp[0][i] = dp[0][i-1] + grid[0][i];
            }
            //填充
            for (int i = 1; i < rowNum; i++) {
                for (int j = 1; j < columnNum; j++) {
                    dp[i][j] = grid[i][j] + Math.min(dp[i-1][j], dp[i][j-1]);
                }
            }
            return dp[rowNum-1][columnNum-1];
        }
    }
}
